/*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
*
*
* Authors:                  zhoujian
* Email:            343600434@qq.com
* Date:         Jan-30-2024 10:48:08
*
*/


#ifndef __flash_H__
#define __flash_H__




#ifdef __cplusplus
extern "C" {
#endif

//
//0x0803 F000 - 0x0803 FFFF 4KB
#define SAVE_ADDR 0x800FC00
#define FLASH_PAGE_SIZE 0x400
#define CHIP_CAPACITY 0x1FFFF7E0

#define ID_ADDR0 0x1FFFF7E8
#define ID_ADDR1 0x1FFFF7EC
#define ID_ADDR2 0x1FFFF7F0

//#define DEBUG_BOOT_GET_FLASH


//#define TEST_WRITE_FLASH

extern uint32_t id[3];
extern uint32_t odo_km_v[2];
extern uint32_t boot_odo_km_v[2];

void flash_init(void);
void flash_task(void);
void flash_deinit(void);
void trip_clean();

void unique_id(void);
void get_boot_odo(void);
FlagStatus write_data_flash(uint32_t* value);
void get_odo_from_flash(void);
void clear_odo_trip();
void save_odo_km_v(void);


typedef struct {
    uint16_t odo;//总里程
    uint8_t mul;//里程倍率
    uint8_t sel;//km miles ft
    uint16_t trip;//单词里程
    uint8_t odo_dam;//10米
    uint8_t trip_dam;//10米
} FLASH_INFO;

extern FLASH_INFO flash_info;


extern FLASH_INFO flash_info_save;

typedef enum {
    FLASH_IDLE,
    FLASH_READY,
    FLASH_ERASE_READY,
    FLASH_ERASE_OK,
    FLASH_WRITE_READY,
    FLASH_WRITE_READY2,
    FLASH_WRITE_OK,
    FLASH_END,
} FLASH_STATUS;


typedef struct {
    uint8_t status;
    uint8_t int_flag;
    uint8_t timeout;
    uint32_t addr;
    uint16_t index;
    uint16_t len;
    uint32_t* dat;
} FLASH_T;

extern FLASH_T flash_st;

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
class flash
{

public:
    flash();
    ~flash();

};
#endif



#endif
